import { createSSRApp } from 'vue'
// import Vue from 'vue';
import App from './App.vue'
import GoEasy from '@/uni_modules/GOEASY-IM/js_sdk/goeasy-2.6.6.esm.min.js'


const goEasy = GoEasy.getInstance({
  host: 'hangzhou.goeasy.io',	//应用所在的区域地址: 【hangzhou.goeasy.io |singapore.goeasy.io】
  appkey: 'BC-c1187292e1fb4848a341ed666700ec40',	// common key,
  modules: ['im'],
  // true表示支持通知栏提醒，false则表示不需要通知栏提醒
  allowNotification: true //仅有效于app,小程序和H5将会被自动忽略
});

goEasy.im.on(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, setUnreadNumber);
function setUnreadNumber(content) {
  let unreadTotal = content.unreadTotal;
  if (unreadTotal > 0) {
    uni.setTabBarBadge({
      index: 1,
      text: unreadTotal.toString()
    });
  } else {
    uni.removeTabBarBadge({ index:1 });
  }
}

goEasy.onClickNotification((message) => {
  let currentUrl;
  const routes = getCurrentPages();

  if (routes && routes.length) {
    const curRoute = routes[routes.length - 1].route;
    const curParam = routes[routes.length - 1].options;
    currentUrl = '/' + curRoute + `?to=${curParam.to}`;
  }

  let newUrl;
  switch (message.toType) {
    case GoEasy.IM_SCENE.PRIVATE:
      newUrl = '/pages/privateChat?to=' + message.senderId;
      break;
    case GoEasy.IM_SCENE.GROUP:
      newUrl = '/pages/groupChat?to=' + message.groupId;
      break;
  }

  if (currentUrl !== newUrl) {
    uni.navigateTo({
      url: newUrl,
    });
  }

});
// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false

// 王鸿权
// 添加路由拦截器
uni.beforeEach((to, from, next) => {
  // 在这里判断是否禁止跳转到特定页面
  if (to.path === '/pages/login/advertise') {
    // 可以在这里处理拒绝跳转的逻辑
    console.log('禁止跳转到广告页面')
    return
  }

  // 如果不需要拦截，继续进行跳转
  next()
})
// 王鸿权

App.mpType = 'app'
const app = new Vue({
	...App
})
app.$mount()
// #endif

// #ifdef VUE3
export function createApp() {
  const app = createSSRApp(App)
  app.provide('GoEasy', GoEasy)
  app.provide('goEasy ', goEasy)
  app.config.globalProperties.GoEasy = GoEasy
  app.config.globalProperties.goEasy = goEasy
  return {
    app
  }
}
// #endif
